﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Http;
using System.Text;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Linq;
using DB_NH;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ywxt.NewsPush.Controllers.Home
{
    public class IndexController : Controller
    {
        //返回页面
        public ActionResult Index()
        {
            return View();
        }
        #region Json定义

        public class JSONClassForGxykdx : Models.clsCommonJsonResult
        {
            public List<Models.Gxykdx.GXBIDModel> VmGXBIDModels { get; set; }

            public JSONClassForGxykdx()
            {
                VmGXBIDModels = new List<Models.Gxykdx.GXBIDModel>();
            }
        }
        #endregion


        #region 查询最新的10条

        public JsonResult GetGxNewList(string WebSite)
        {
            var _returnValue = new JSONClassForGxykdx();
            var sessionFactory = SessionFactory.GetCurrentFactory();
            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                try
                {
                    var _gx =
                    (from v in session.Query<DB_NH.DB_Model.Gxzfcg.gxykdx>()
                     where v.IsValid == 1
                     && v.Website == WebSite
                     orderby v.Time descending
                     select new Models.Gxykdx.GXBIDModel()
                     {
                         GId = v.Id,
                         Title = v.Title,
                         Url = v.Url,
                         Time = v.Time,
                         Website = v.Website,
                         WebUrl = v.WebUrl,
                         WebTitle = v.WebTitle,
                         WebNode = v.WebNode

                     }).Take(10);
                    transaction.Commit();
                }
                catch (Exception _ex)
                {
                    if (transaction.IsActive) transaction.Rollback();
                    _returnValue.ISOK = false;
                    _returnValue.MESSAGE = _ex.Message;
                }
            }
            return Json(_returnValue);
        }
        #endregion

        #region 查询各类栏目/总列表

        public JsonResult GetGxList(string keyword, string WebSite, int pagesize, int pageindex)
        {
            var _returnValue = new JSONClassForGxykdx();
            var sessionFactory = SessionFactory.GetCurrentFactory();
            using (var session = sessionFactory.OpenSession())
            using (var transaction = session.BeginTransaction())
            {
                try
                {
                    if (WebSite != "" && WebSite != "")
                    {
                        if (keyword != "" && keyword != null)
                        {
                            var _gx =
                           (from v in session.Query<DB_NH.DB_Model.Gxzfcg.gxykdx>()
                            where v.IsValid == 1
                            && v.Website == WebSite
                            && v.Title.Contains(keyword)
                            orderby v.Time descending
                            select new Models.Gxykdx.GXBIDModel()
                            {
                                GId = v.Id,
                                Title = v.Title,
                                Url = v.Url,
                                Time = v.Time,
                                Website = v.Website,
                                WebUrl = v.WebUrl,
                                WebTitle = v.WebTitle,
                                WebNode = v.WebNode

                            });
                            //计算查询的总记录数
                            _returnValue.RecordCount = _gx.Count();

                            if (pagesize > 0 && pagesize > 0 && _returnValue.RecordCount > 0)
                            {
                                _returnValue.PageCount = _returnValue.RecordCount % pagesize == 0 ? _returnValue.RecordCount / pagesize : (_returnValue.RecordCount / pagesize + 1);

                                if (pageindex < 0)
                                {
                                    pageindex = 0;
                                }
                                else if (pageindex >= _returnValue.PageCount)
                                {
                                    pageindex = _returnValue.PageCount - 1;
                                }

                                _returnValue.PageIndex = pageindex;

                                _returnValue.VmGXBIDModels = _gx
                                    .Skip(pageindex * pagesize)
                                    .Take(pagesize)
                                    .ToList();
                            }
                            else
                            {
                                _returnValue.PageCount = 1;
                                _returnValue.PageIndex = 0;

                                _returnValue.VmGXBIDModels = _gx
                                    .ToList();
                            }
                        }
                    }
                    else
                    {
                        if (keyword != "" && keyword != null)
                        {
                            var _gx =
                           (from v in session.Query<DB_NH.DB_Model.Gxzfcg.gxykdx>()
                            where v.IsValid == 1
                            && v.Title.Contains(keyword)
                            orderby v.Time descending
                            select new Models.Gxykdx.GXBIDModel()
                            {
                                GId = v.Id,
                                Title = v.Title,
                                Url = v.Url,
                                Time = v.Time,
                                Website = v.Website,
                                WebUrl = v.WebUrl,
                                WebTitle = v.WebTitle,
                                WebNode = v.WebNode

                            });
                            //计算查询的总记录数
                            _returnValue.RecordCount = _gx.Count();

                            if (pagesize > 0 && pagesize > 0 && _returnValue.RecordCount > 0)
                            {
                                _returnValue.PageCount = _returnValue.RecordCount % pagesize == 0 ? _returnValue.RecordCount / pagesize : (_returnValue.RecordCount / pagesize + 1);

                                if (pageindex < 0)
                                {
                                    pageindex = 0;
                                }
                                else if (pageindex >= _returnValue.PageCount)
                                {
                                    pageindex = _returnValue.PageCount - 1;
                                }

                                _returnValue.PageIndex = pageindex;

                                _returnValue.VmGXBIDModels = _gx
                                    .Skip(pageindex * pagesize)
                                    .Take(pagesize)
                                    .ToList();
                            }
                            else
                            {
                                _returnValue.PageCount = 1;
                                _returnValue.PageIndex = 0;

                                _returnValue.VmGXBIDModels = _gx
                                    .ToList();
                            }
                        }
                    }

                    transaction.Commit();
                }

                catch (Exception _ex)
                {
                    if (transaction.IsActive) transaction.Rollback();
                    _returnValue.ISOK = false;
                    _returnValue.MESSAGE = _ex.Message;
                }
            }
            return Json(_returnValue);
        }

        #endregion

        #region 查询总列表

        //public JsonResult GetAllGxList(string keyword, int pagesize, int pageindex)
        //{
        //    var _returnValue = new JSONClassForGxykdx();
        //    var sessionFactory = SessionFactory.GetCurrentFactory();
        //    using (var session = sessionFactory.OpenSession())
        //    using (var transaction = session.BeginTransaction())
        //    {
        //        try
        //        {
        //            if (keyword != "" && keyword != null)
        //            {
        //                var _gx =
        //               (from v in session.Query<DB_NH.DB_Model.Gxzfcg.gxykdx>()
        //                where v.IsValid == 1
        //                && v.Title.Contains(keyword)
        //                orderby v.CreateDate descending
        //                select new Models.Gxykdx.GXBIDModel()
        //                {
        //                    GId = v.Id,
        //                    Title = v.Title,
        //                    Url = v.Url,
        //                    Time = v.Time,
        //                    Website = v.Website,
        //                    WebUrl = v.WebUrl,
        //                    WebTitle = v.WebTitle,
        //                    WebNode = v.WebNode

        //                });
        //                //计算查询的总记录数
        //                _returnValue.RecordCount = _gx.Count();

        //                if (pagesize > 0 && pagesize > 0 && _returnValue.RecordCount > 0)
        //                {
        //                    _returnValue.PageCount = _returnValue.RecordCount % pagesize == 0 ? _returnValue.RecordCount / pagesize : (_returnValue.RecordCount / pagesize + 1);

        //                    if (pageindex < 0)
        //                    {
        //                        pageindex = 0;
        //                    }
        //                    else if (pageindex >= _returnValue.PageCount)
        //                    {
        //                        pageindex = _returnValue.PageCount - 1;
        //                    }

        //                    _returnValue.PageIndex = pageindex;

        //                    _returnValue.VmGXBIDModels = _gx
        //                        .Skip(pageindex * pagesize)
        //                        .Take(pagesize)
        //                        .ToList();
        //                }
        //                else
        //                {
        //                    _returnValue.PageCount = 1;
        //                    _returnValue.PageIndex = 0;

        //                    _returnValue.VmGXBIDModels = _gx
        //                        .ToList();
        //                }
        //            }


        //            transaction.Commit();
        //        }
        //        catch (Exception _ex)
        //        {
        //            if (transaction.IsActive) transaction.Rollback();
        //            _returnValue.ISOK = false;
        //            _returnValue.MESSAGE = _ex.Message;
        //        }
        //    }
        //    return Json(_returnValue);
        //}

        #endregion
    }
}